var _iSpacerWidth = 2;

function handleResize(o)
{
	try
	{	
		// Get the TD to be resized
		var iCell = o.parentElement.nextSibling.cellIndex;
		
		// Get the TD's <COL> Tag
		var oCol = gridBarCols.children[iCell];
		
		// Cache the width and offset
		var iWidth = parseInt(oCol.width, 10);
		var iOffsetX = event.offsetX;
		var iLeftColDifference = 0;
		
		// Add or subtract the difference of the mouse movement
		// This determines and auto-corrects resize requests to be "valid"
		if (iOffsetX > 0)
		{		
			iWidth = iWidth - iOffsetX;
		}
		else
		{
			iLeftColDifference = iOffsetX;
			iOffsetX =  Math.abs(iOffsetX);

			// Check if this is an auto-expander row
			if (oCol.previousSibling.previousSibling === colAutoExpander)
			{
				// Do not resize the left column
				iLeftColDifference = 0;

				// If the resize amount requested would make the auto expander smaller than 1,
				// change the resize amount to the "best" possible value
				if (colAutoExpander.offsetWidth - iOffsetX < 1)
				{
					iOffsetX = (colAutoExpander.offsetWidth - iOffsetX) + iOffsetX - 5;
				}
			}// Otherwise it is not an auto-expander, prevent a left column from being resized smaller than 5
			else if (oCol.previousSibling.previousSibling.offsetWidth - iOffsetX < 1)
			{
				iLeftColDifference = (oCol.previousSibling.previousSibling.offsetWidth * -1) + 5;
				iOffsetX = (oCol.previousSibling.previousSibling.offsetWidth - iOffsetX) + iOffsetX - 5;
			}

			iWidth = iWidth + iOffsetX;
		}
		
		// Prevent columns from becoming too small
		// We check the width later on to determine if this is a spacer column or not
		// Adding 1 to the spacer width so that this does not appear as a spacer column to the getCols() code
		iWidth =  Math.max(iWidth, _iSpacerWidth + 1);


		// If the left column has changed, then update it
		if (iLeftColDifference != 0)
		{
			oCol.previousSibling.previousSibling.width = parseInt(oCol.previousSibling.previousSibling.width,10) + iLeftColDifference;
		}

		// Set the <COL> Tag Widths
		oCol.width = iWidth;

		// If there is data, then resize the data grid
		if (_grid.gridBodyTable.rows[0].oid)
		{
			_grid.gridBodyTable.firstChild.children[FindBarColOffset(iCell) + FindDataColOffset()].width = (iWidth + _iSpacerWidth);

			// If the left column has changed, then update it in the Grid
			if (iLeftColDifference != 0)
			{
				var oTmp = _grid.gridBodyTable.firstChild.children[FindBarColOffset(iCell) + FindDataColOffset()];
				oTmp.previousSibling.width = parseInt(oTmp.previousSibling.width,10) + iLeftColDifference;
			}

			_grid.gridBodyTable.focus();
		}

	// Get around IE Bug when there is a horizontal scrollbar
	gridBar.scrollIntoView();

	// Cache the column info in the form
	frmGrid.cols.value = getCols();

	}
	catch (e)
	{
		// Do Nothing - The resize will gracefully not do anything.
	}
}

// Find the Index of the COL being resized, relative to the auto-expander cell
function FindBarColOffset(iCell)
{
	var ii = 0;

	// Work backwards because it is the most "known" scenario:
	// Refresh Button, then | then Column
	for (var i = iCell; i > -1; i--)
	{
		if (gridBar.rows[0].cells[i].className == "sort")
		{			
			if (gridBar.rows[0].cells[i].isAutoExpander)
			{
				return ii;
			}
			else
			{
				ii++;
			}
		}
	}
	
	return ii;
}


// Find the index of the COL to be resized, relative to the auto-expander cell, icon and preview button
function FindDataColOffset()
{
	var iOffset = 0;

	// Work forwards
	for (var i = 0; i < _grid.gridBodyTable.rows[0].cells.length; i++)
	{
		if (_grid.gridBodyTable.rows[0].cells[i].firstChild.tagName == "IMG")
		{
			iOffset++;
		}
	}
	
	return iOffset;
}


// Builds the COLs param used to support columns across pages/sorts/refreshes
function getCols()
{
	var s = "*";
	var o = gridBar.firstChild.children;

	var iStartOffset = 4; // Checkbox + Spacer + First Col + Auto Expander
	var iEndOffset = 3; // Refresh + Spacer + 1 since length is non-zero length

	for (var i = iStartOffset; i < o.length - iEndOffset; i++)
	{
		if (o[i + 1].width != _iSpacerWidth)
		{
			s += "," + (parseInt(o[i + 1].width, 10) + _iSpacerWidth);
		}
	}

	return s;
}